package com.cjm.one.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.goods.service.GoodsCategoryService;
import com.cjm.one.goods.mapper.GoodsCategoryMapper;
import com.cjm.one.model.goods.entity.GoodsCategory;
import com.cjm.one.model.goods.vo.GoodsCategoryVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29289
* @description 针对表【goods_category(商品分类)】的数据库操作Service实现
* @createDate 2024-09-05 16:06:59
*/
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory>
    implements GoodsCategoryService {
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategoryVo> pageCategory( String searchKey) {
        List<GoodsCategoryVo> page = goodsCategoryMapper.pageCategory(searchKey);
        if (page.size() > 0) {
            List<GoodsCategoryVo> goodsCategoryVos = treeCategory(page, 0L);
            page=(goodsCategoryVos);
        }
        return page;
    }

    @Override
    public Boolean removeGoodsCategoryById(Long id) {
        LambdaQueryWrapper<GoodsCategory> goodsCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCategoryLambdaQueryWrapper.eq(GoodsCategory::getParentId, id).last("limit 1");
        GoodsCategory goodsCategory = goodsCategoryMapper.selectOne(goodsCategoryLambdaQueryWrapper);
        if (goodsCategory != null) {
            throw new GlobalException(ResultCodeEnum.DELETE_GOODS_CATEGORY_ERROR);
        }
        int i = goodsCategoryMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public List<GoodsCategory> selectToParentId(Long parentId) {
        if (parentId == null) { parentId = 0L; }
        LambdaQueryWrapper<GoodsCategory> goodsCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCategoryLambdaQueryWrapper.eq(GoodsCategory::getParentId, parentId);
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectList(goodsCategoryLambdaQueryWrapper);
        return goodsCategories;
    }

    @Override
    public Result<List<GoodsCategory>> listLimit10(Integer size) {

        LambdaQueryWrapper<GoodsCategory> goodsCategoryVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCategoryVoLambdaQueryWrapper.orderByAsc(GoodsCategory::getOrderNum);
        if (size != null){
            goodsCategoryVoLambdaQueryWrapper.last("limit " + size);
        }
        goodsCategoryVoLambdaQueryWrapper.eq(GoodsCategory::getParentId, 0).eq(GoodsCategory::getStatus, 0);

        return  Result.ok(goodsCategoryMapper.selectList(goodsCategoryVoLambdaQueryWrapper));
    }

    public List<GoodsCategoryVo> treeCategory(List<GoodsCategoryVo> list, Long prentId) {
        ArrayList<GoodsCategoryVo> objects = new ArrayList<>();
        if (list.size() > 0) {
            for (GoodsCategoryVo goodsCategory : list) {
                if (goodsCategory.getParentId().equals(prentId)) {
                    List<GoodsCategoryVo> children = treeCategory(list, goodsCategory.getId());
                    goodsCategory.setChildren(children);
                    objects.add(goodsCategory);
                }
            }
        }
        return objects;
    }
}




